<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/10
 * Time: 14:58
 */

namespace App\Api\Admin\Controllers;

use App\Exceptions\ApiException;
use Illuminate\Http\Request;
use App\Services\Admin;
use App\Models\ReportType;
use App\Models\Admin\Log;
use App\Models\admin\Info;
use App\Models\Config;

class SystemController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        if ($this->user['permissions'] !== '*' && !isset($this->user['permissions']['admin_permission'])) {
            throw new ApiException('没有权限', 0);
        }
    }
    public function index(){
        $data = D('config')->get(['id','tips','value','download_links','ask_value','make_content']);
        return json_success('ok',$data);
    }
    public function modifylink(Request  $request){
        $id = IT('id',true,'请选择ID');
        $app_data = [
          'value' => $request->input('value'),
          'download_links'=>$request->input('download_links'),
          'ask_value'=> $request->input('ask_value'),
          'make_content'=>$request->input('make_content'),
        ];
        $config_data =Config::where('id','=',$id)->first();
        $data = $config_data->update($app_data);
        if($data){
            if($id ==1){
                $system_name= 'Android';
            }else{
                $system_name= 'ios';
            }
            (new Log)->add($this->user, '修改了'.$system_name.'版本最新链接');
            return json_success('ok');
        }else {
            return json_error('修改失败');
        }
    }
    public function about(){
        $about_data = Info::get(['id','content']);
        return json_success('ok',$about_data);
    }
    public  function  UpdateAbout(){
     $id = IT('id',true, '请输入ID');
     $content = IT ('content',true,'请输入你要修改的内容');
     $about_data = Info::where('id','=',$id)->first();
     $data = $about_data->update(['content'=>$content]);
     if($data){
         (new Log)->add($this->user, '修改了公司简介');
         return json_success('ok');
     }else{
         return json_error('修改失败');
     }
    }
    /**
     * 举报栏目列表
     * @return array
     */
    public function Reporttype(){
        $video_type= ReportType::where('type','=',1)->get(['id','name']);
        $people_type= ReportType::where('type','=',2)->get(['id','name']);
        return json_success('ok',['video_type'=>$video_type,'people_type'=> $people_type]);
    }
    /**
     * 添加举报栏目
     * @return array
     * @throws \Exception
     *
     */
    public function addReport(){
        $name = IT('name',true, '请输入栏目名称');
        $type = IT('type',true,'请输入类型');
        $data= [
            'name'=>$name,
            'type'=>intval($type),
            'created_at' => time2date()
        ];
        $select = ReportType::where('name','=',$name)->first();
        if($select){
           return json_error('此栏目已存在');
        }
        if(is_numeric($type)){
           return json_error('举报的类型，不正确');
        }
        $insert_data=  ReportType::insert($data);
        if($insert_data){
            (new Log)->add($this->user, '添加了举报栏目-'.$name);
            return json_success('ok');
        }else{
            return json_error('添加失败');
        }
    }

    /**
     * 删除举报栏目
     * @return array
     * @throws \Exception
     */
    public function DelReport(){
        $name = IT('name',true, '请输入栏目名称');
        $type = IT('type',true,'请输入类型');
        $select = ReportType::where('name','=',$name)->first();
        if(!$select){
          return  json_error('没有此栏目');
        }
        $del_data = ReportType::where('name','=',$name)->where('type','=',$type)->delete();
        if($del_data){
            (new Log)->add($this->user, '删除了举报栏目名称为-'.$name);
            return json_success('ok');
        }else{
            return json_error('删除失败');
        }
    }

    /**
     * @return array
     * 影片时长
     */
    public function MovieLength(){
        $data = D('movie_length')->first();
        return json_success('ok',$data);
    }
    public function mofifyMlength(){
        $value = IT('content',true,'请输入时长');
       if(!is_numeric($value)){
         return json_error('时间长度必须是数字');
        }
        $data = D('movie_length')->where('id','=',1)->update(['content'=>$value,'updated_at'=>time2date()]);
        if($data){
            (new Log)->add($this->user, '修改了影片时长为-'.$value);
            return json_success('ok');
        }else{
            return json_error('修改失败');
        }
    }





}